package com.example.demo.domain.userInfo;

import java.util.List;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;

import com.example.demo.domain.role.Role;

public class UserInfo {
	private static final long serialVersionUID = -6743567631108323096L;

	private Integer uid;

	private String name;

	private String password;

	private String salt;

	private Integer state;

	private Integer position;

	private String username;

	/** 角色集合 */
	private List<Role> roles;

	/** 角色组 */
	private Integer[] roleIds;

	public Integer getUid() {
		return uid;
	}

	public void setUid(Integer uid) {
		this.uid = uid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name == null ? null : name.trim();
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password == null ? null : password.trim();
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt == null ? null : salt.trim();
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public Integer getPosition() {
		return position;
	}

	public void setPosition(Integer position) {
		this.position = position;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username == null ? null : username.trim();
	}

	public String getCredentialsSalt() {

		return this.username + this.salt;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public Integer[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Integer[] roleIds) {
		this.roleIds = roleIds;
	}

	public static boolean isAdmin(Integer userId) {
		return userId != null && 1L == userId;
	}

	public void randomSalt() {
		// 一个Byte占两个字节，此处生成的3字节，字符串长度为6
		SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator();
		String hex = secureRandom.nextBytes(3).toHex();
		setSalt(hex);
	}

	public boolean isAdmin() {
		return isAdmin(this.uid);
	}
}